import {
  getProductslist,
  getCategoryList
} from '../../api/store.js';

import {
  getBanners
} from '../../api/api.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showType:1,//1专辑 2周边goods 4fansland  1,2搜索
    productList: [],
    scrollTop: 0,
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': "fans'land",
    },
    navH: "",
    is_switch: false,
    where: {
      source: 1,
      sid: 0,
      keyword: '',
    //   salesOrder: '',
      is_hot: 0,
      books: '',
      news: "",
      page: 1,
      limit: 10,
    },
    hots: 0,
    isHide: false,
    books: false,
    news: false,
    loadend: false,
    loading: false,
    loadTitle: '加载更多',
    userInfo: {},
    banners: [],
    showCate:false,
    category:'全部',
    categoryList:[]
  },
  onLoadFun: function (e) {


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ['parameter.title']: options.title || '商品列表',
      ['where.keyword']: options.searchValue || '',
      ['where.source']: options.type || '',
      navH: app.globalData.navHeight,
      isHide:options.isHide,
      showType:options.type
    });

    this.get_product_list();
    this.getBanners(this.data.showType)
    this.getCategoryList()

  },
  getCategoryList(){
    getCategoryList({limit:9999}).then(res=>{
      let list = [{id:0,cate_name:'全部'}]
      list.push(...res.data)
      this.setData({
        categoryList:list
      })
    })
  },
  getBanners(type) {
    let that = this
    getBanners().then(res => {
      let banners = []
      switch (type) {
        case "1": //专辑
          banners = res.data.album_banner
          break
        case "2": //周边
          banners = res.data.goods_banner
          break
        case "4": //fansland
          banners = res.data.fansi_banner
          break
      }
      that.setData({
        banners: banners
      })
    })
  },
  /**
   * 商品详情跳转
   */
  goDetail: function (e) {
    let item = e.currentTarget.dataset.items;
    wx.navigateTo({
      url: `/pages/goods_details_new/index?id=${item.id}&isHide=${this.data.isHide}&title=fans'land`
    });
  },
  Changswitch: function () {
    var that = this;
    that.setData({
      is_switch: !this.data.is_switch
    })
  },
  searchSubmit: function (e) {
    var that = this;
    this.setData({
      ['where.keyword']: e.detail.value,
      loadend: false,
      ['where.page']: 1
    })
    this.get_product_list(true);
  },

  //点击事件处理
  set_where: function (e) {
    var dataset = e.target.dataset;
    switch (dataset.type) {
        case '1': //最新
            if (this.data.news) {
                this.setData({news: !this.data.news});
            } else {
                this.setData({
                    news: !this.data.news,
                    books: false,
                    hots: 0,
                });
            }
            break;
        case '2':
            if (this.data.hots == 0) {
                this.data.hots = 1;
            } else if (this.data.hots == 1) {
                this.data.hots = 0;
            }
            if (this.data.hots == 1) {
                this.setData({
                    hots:this.data.hots,
                    news: false,
                    books: false,
                });
            } else {
                this.setData({hots:this.data.hots});
            }
            break;
        case '3':
            if (this.data.books) {
                this.setData({books: !this.data.books});
            } else {
                this.setData({
                    books: !this.data.books,
                    news: false,
                    hots: 0,
                });
            }
            break;
    }
    this.setData({
      loadend: false,
      ['where.page']: 1
    });
    this.get_product_list(true);
  },
  //设置where条件
  setWhere: function () {
    if (this.data.hots == 0) 
    this.data.where.is_hot = 0;
  else if (this.data.hots == 1)
    this.data.where.is_hot = 1;
    this.data.where.news = this.data.news ? 1 : 0;
    this.data.where.books = this.data.nebooksws ? 1 : 0;
    this.setData({
      where: this.data.where
    });
  },
  //查找产品
  get_product_list: function (isPage) {
    let that = this;
    this.setWhere();
    if (that.data.loadend) return;
    if (that.data.loading) return;
    if (isPage === true) that.setData({
      productList: []
    });
    that.setData({
      loading: true,
      loadTitle: ''
    });
    getProductslist(that.data.where).then(res => {
      let list = res.data.data;
      let productList = app.SplitArray(list, that.data.productList);
      let loadend = list.length < that.data.where.limit;
      that.setData({
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '已全部加载' : '加载更多',
        productList: productList,
        ['where.page']: that.data.where.page + 1,
      });
    }).catch(err => {
      that.setData({
        loading: false,
        loadTitle: '加载更多'
      });
    });
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      ['where.page']: 1,
      loadend: false,
      productList: []
    });
    this.get_product_list();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.get_product_list();
  },
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })

  },
  hideCate(){
    this.setData({showCate:false})
  },
 showCate(){
    this.setData({showCate:true})
  },
  onCateClick(e){
    let item = e.currentTarget.dataset.item
    this.setData({
      'where.sid':item.id,
      ['where.page']: 1,
      loadend: false,
      productList: [],
      showCate:false,
      category:item.cate_name
    })
    this.get_product_list();
  }
})